1361A - Johnny and Contribution - CodeForces Solution


constructive algorithms graphs greedy sortings *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define ll long long
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define forn1(i, n) for (int i = 1; i <= (n); ++i)
#define pb push_back
#define max(a,b) ((a)>(b)? (a):(b))
#define min(a,b) ((a)<(b)? (a):(b))
#define all(v) v.begin(),v.end()
#define rall(x) (x).rbegin(), (x).rend()
const int mod = 1e9+7 ;

void print_vector(vector<int>v){
    for(int i=0;i<v.size();i++){
        cout<<v[i]<<" ";
    }
    cout<<endl;
}
void print_arr(int arr[], int n){
    for(int i=0;i<n;i++){
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}
void print_vector_pair(vector<pair<int,int>>v){
    for(int i=0;i<v.size();i++){
        cout<<v[i].first<<" "<<v[i].second<<" "<<endl;
    }
    cout<<endl;
}

// int kmp(string s){
//     vector<int>lps(s.size(),0);
//     for(int i=1;i<lps.size();i++){
//         int prev_index = lps[i-1];
//         while(prev_index>0 && s[i]!=s[prev_index]){
//             prev_index = lps[prev_index-1];
//         }
//         lps[i] = prev_index + (s[i]==s[prev_index]?1:0);
//     }
//     return lps[lps.size()-1];
// }



void solve(){
    int n,m;
    cin>>n>>m;
    vector<int>gr[n+1];
    for(int i=0;i<m;i++){
        int u,v;
        cin>>u>>v;
        gr[u].pb(v);
        gr[v].pb(u);
    }
        vector<ll> v[n + 1];
    ll colour_graph[n + 1];
    for (ll i = 1; i <= n; i++) {
        ll colour;
        cin >> colour;
 
        colour_graph[i] = colour;
        v[colour].push_back(i);
    }
 
    ll flag = 0;
    vector<ll> result;
    for (ll i = 1; i <= n; i++) {
        for (auto nodes : v[i]) {
 
            set<ll> s;
            for (auto neigh : gr[nodes]) {
                if (colour_graph[neigh] < colour_graph[nodes]) {
                    s.insert(colour_graph[neigh]);
                }
                if (colour_graph[neigh] == colour_graph[nodes]) {
                    flag = 1;
                    break;
                }
            }
 
            if (s.size() != colour_graph[nodes] - 1) {
                flag = 1;
                break;
            }
 
            result.push_back(nodes);
        }
    }
 
    if (flag == 1) {
        cout << "-1" << "\n";
    } else {
        for (ll i = 0; i < result.size(); i++) {
            cout << result[i] << " ";
        }
    }

}  

signed main(){
    IOS
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int tests;
    tests = 1;
    // cin>>tests;
    while(tests--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix